package com.xkdx.caipiao.wiget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.LinearLayout;
import android.widget.Scroller;

public class MyFlipperLayout extends LinearLayout {

	public static String TAG = "MyFlipperLayout";
	private Scroller mScroller;
	private VelocityTracker mVelocityTracker;
	private Context context;
	public static final int SCREEN_STATE_CLOSE = 0;
	public static final int SCREEN_STATE_OPEN = 1;
	public static final int TOUCH_STATE_RESTART = 2;
	public static final int TOUCH_STATE_SCROLLING = 3;
	public static final int SCROLL_STATE_NO_ALLOW = 4;
	public static final int SCROLL_STATE_ALLOW = 5;
	private int mScreenState = 0;
	private int mTouchState = 0;
	private int mScrollState = 0;
	private int mVelocityValue = 0;
	private boolean mOnClick = false;

	public MyFlipperLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mScroller = new Scroller(context);
	}

	public MyFlipperLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		mScroller = new Scroller(context);
	}

	public MyFlipperLayout(Context context) {
		super(context);
		mScroller = new Scroller(context);
	}

	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		for (int i = 0; i < getChildCount(); i++) {
			View child = getChildAt(i);
			int height = child.getMeasuredHeight();
			int width = child.getMeasuredWidth();
			child.layout(0, 0, width, height);
		}
	}

	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int width = MeasureSpec.getSize(widthMeasureSpec);
		int height = MeasureSpec.getSize(heightMeasureSpec);
		setMeasuredDimension(width, height);
		for (int i = 0; i < getChildCount(); i++) {
			getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
		}
	}

	public boolean dispatchTouchEvent(MotionEvent ev) {
		obtainVelocityTracker(ev);
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			Log.i(TAG, "dispatchTouchEvent->down");
			mTouchState = mScroller.isFinished() ? TOUCH_STATE_RESTART
					: TOUCH_STATE_SCROLLING;
			if (mTouchState == TOUCH_STATE_RESTART) {
				mOnClick = true;
			} else {
				mOnClick = false;
			}
			break;
		case MotionEvent.ACTION_MOVE:
			Log.i(TAG, "dispatchTouchEvent->move");
			mVelocityTracker.computeCurrentVelocity(1000,
					ViewConfiguration.getMaximumFlingVelocity());
			if (mScrollState == SCROLL_STATE_ALLOW) {
				return true;
			}
			break;
		case MotionEvent.ACTION_UP:
			
			break;
		}
		return super.dispatchTouchEvent(ev);
	}

	public boolean onInterceptTouchEvent(MotionEvent ev) {
		obtainVelocityTracker(ev);
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			Log.i(TAG, "onInterceptTouchEvent->down");
			mTouchState = mScroller.isFinished() ? TOUCH_STATE_RESTART
					: TOUCH_STATE_SCROLLING;
			if (mTouchState == TOUCH_STATE_RESTART) {
				mOnClick = true;
				return true;
			}
			break;

		case MotionEvent.ACTION_MOVE:
			
			
			break;

		case MotionEvent.ACTION_UP:
			Log.i(TAG, "onInterceptTouchEvent->up");
			releaseVelocityTracker();
			if (mScrollState == SCROLL_STATE_ALLOW
					&& mScreenState == SCREEN_STATE_OPEN) {
				return true;
			}
			break;
		}
		return super.onInterceptTouchEvent(ev);
	}

	public boolean onTouchEvent(MotionEvent event) {
		obtainVelocityTracker(event);
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mTouchState = mScroller.isFinished() ? TOUCH_STATE_RESTART
					: TOUCH_STATE_SCROLLING;
			if (mTouchState == TOUCH_STATE_RESTART) {
				return true;
			}
			break;

		case MotionEvent.ACTION_MOVE:
			Log.i(TAG, "onTouchEvent->move");
			mVelocityTracker.computeCurrentVelocity(1000,
					ViewConfiguration.getMaximumFlingVelocity());
			mVelocityValue = (int) mVelocityTracker.getYVelocity();
			getChildAt(0).scrollTo(0, -(int) event.getY());
			break;

		case MotionEvent.ACTION_UP:
			Log.i("hz", mVelocityValue + "");
			// if (mVelocityValue > 2000) {
			// invalidate();
			//
			// } else if (mVelocityValue < -2000) {
			// invalidate();
			// } else if (event.getY() < getHeight() / 2) {
			// invalidate();
			// } else {
			// invalidate();
			// }
			break;
		}
		return super.onTouchEvent(event);
	}

	public void computeScroll() {
		super.computeScroll();
		if (mScroller.computeScrollOffset()) {
			getChildAt(1).scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
			postInvalidate();
		}
	}

	private void obtainVelocityTracker(MotionEvent event) {
		if (mVelocityTracker == null) {
			mVelocityTracker = VelocityTracker.obtain();
		}
		mVelocityTracker.addMovement(event);
	}

	private void releaseVelocityTracker() {
		if (mVelocityTracker != null) {
			mVelocityTracker.recycle();
			mVelocityTracker = null;
		}
	}

}
